package com.test.burnincash.aaahpoppomelo.activity;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.test.burnincash.R;
import com.test.burnincash.aaahpoppomelo.base.BaseActivity_hpoppomelo;
import com.test.burnincash.aaahpoppomelo.bean.Order_hpoppomelo;
import com.test.burnincash.aaahpoppomelo.bean.UserData_hpoppomelo;
import com.test.burnincash.util_burnin.StringUtil_burnin;

import java.util.ArrayList;

public class OrderManagementActivity_hpoppomelo extends BaseActivity_hpoppomelo {

    private LinearLayout groupLayout;

    private EditText orderNumEt,itemNameEt;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_order_management_hpoppomelo);
        init();
    }

    @Override
    protected void onResume() {
        super.onResume();
        updateOrders();
    }

    private void init(){
        setBackBtn_burnin(findViewById(R.id.activity_order_management_ib_back));

        orderNumEt = findViewById(R.id.activity_order_management_et_order_num);
        itemNameEt = findViewById(R.id.activity_order_management_et_item_name);
        groupLayout = findViewById(R.id.activity_order_management_layout_group);

        findViewById(R.id.activity_order_management_layout_inquire).setOnClickListener(v -> updateOrders());

    }

    private void updateOrders(){

        groupLayout.removeAllViews();

        String searchOrderNumInput = StringUtil_burnin.getSafeString(orderNumEt.getText().toString());
        String searchItemName = StringUtil_burnin.getSafeString(itemNameEt.getText().toString());

        int searchOrderNum = StringUtil_burnin.safeParseInt(searchOrderNumInput);

        ArrayList<Order_hpoppomelo> orders = UserData_hpoppomelo.getInstance().getOrders();

        if (TextUtils.isEmpty(searchOrderNumInput)&&TextUtils.isEmpty(searchItemName)){

            for (Order_hpoppomelo order:orders){
                showOrder(order);
            }

        }else if (TextUtils.isEmpty(searchItemName)){

            for (Order_hpoppomelo order:orders){
                if (order.getOrderNumber()==searchOrderNum) showOrder(order);
            }

        }else  if (TextUtils.isEmpty(searchOrderNumInput)){

            for (Order_hpoppomelo order:orders){
                if (order.containsItemName(searchItemName)) showOrder(order);
            }

        }else {

            for (Order_hpoppomelo order:orders){
                if (order.getOrderNumber()==searchOrderNum && order.containsItemName(searchItemName)) showOrder(order);
            }

        }

    }

    private void showOrder(Order_hpoppomelo order){
        @SuppressLint("InflateParams") View view = LayoutInflater.from(this).inflate(R.layout.item_order_management_hpoppomelo,null);

        TextView orderNumTv = view.findViewById(R.id.view_order_management_item_tv_order_num);
        TextView dateTv = view.findViewById(R.id.view_order_management_item_tv_date);
        TextView customerTv = view.findViewById(R.id.view_order_management_item_tv_customer);

        orderNumTv.setText(String.valueOf(order.getOrderNumber()));
        dateTv.setText(order.getDate());
        customerTv.setText(order.getCustomer());

        view.findViewById(R.id.view_order_management_item_btn_delete).setOnClickListener(v -> {
            UserData_hpoppomelo.getInstance().getOrders().remove(order);
            updateOrders();
            UserData_hpoppomelo.getInstance().uploadData();
        });

        view.findViewById(R.id.view_order_management_item_btn_edit).setOnClickListener(v -> {
            Order_hpoppomelo.optionOrder = order;
            startActivity_burnin(EditOrderActivity_hpoppomelo.class);
        });

        groupLayout.addView(view);
    }

}